package com.bdqn.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bdqn.dto.Msg;
import com.bdqn.model.RoleList;
import com.bdqn.service.RoleManageService;

@RequestMapping("/RoleManage")
@Controller
public class RoleManageController {

	@Autowired
	private RoleManageService roleManageService;
	
	
	@RequestMapping("/queryRole")
	public String queryRole(HttpServletRequest request) {
		List<RoleList> rolelist=roleManageService.queryRole();
		if(rolelist != null) {
			request.getSession().setAttribute("rolelist", rolelist);
		}
		return "roleManage";
	}
	
	@RequestMapping("/updateRole/{val}/{select}")
	@ResponseBody
	public Msg updateData(@PathVariable("val")String roleName, @PathVariable("select")String state) {
		Msg msg=new Msg();
		if(roleName != null && state != null) {
			boolean flag=roleManageService.updateData(roleName,state);
			if(!flag) {
				msg.setName("修改数据失败,请重新操作!");
			}else {
				msg.setName("修改数据成功!");
			}
		}else {
			msg.setName("数据不能为空");
		}	
		return msg;
	}
	
	@RequestMapping("/deleteData/{content}")
	@ResponseBody
	public Msg deleteData(@PathVariable("content")String roleName) {
		Msg msg=new Msg();
		if(roleName != null ) {
			boolean flag=roleManageService.deleteData(roleName);
			if(!flag) {
				msg.setName("删除角色失败!");
			}else {
				msg.setName("删除角色成功!");
			}
		}else {
			msg.setName("数据不能为空");
		}
		
		return msg;
	}
	
	@RequestMapping("/addRoleData/{role}/{selecttate}")
	@ResponseBody
	public Msg addRoleData(@PathVariable("role")String roleName,@PathVariable("selecttate")String state ) {
		Msg msg= new Msg();
		if(roleName != null && state != null) {
			boolean flag=roleManageService.addRoleData(roleName,state);
			if(!flag) {
				msg.setName("添加角色失败!");
			}else {
				msg.setName("添加角色成功!");
			}
		}else {
			msg.setName("数据不能为空!");
		}
		
		return msg;
	}
}
